﻿using System;
using System.Linq;
using System.Web.UI;
using Ext.Net;
using Sharp.Portal.ExtNet.Extend;
using Sharp.Portal.ExtNet.FormField;
using Sharp.Portal.Mvc.List;

namespace Ext.Net
{
    /// <summary>
    /// Ext扩展
    /// </summary>
    public static class ExtExtension
    {
        /// <summary>
        /// 列表页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static SharpDataList.Builder SharpDataList(this BuilderFactory input)
        {
            var sharpDataList = new Sharp.Portal.ExtNet.Extend.SharpDataList();

            return sharpDataList.ToBuilder();
        }
        /// <summary>
        /// 外键选取框
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static ForeignSelectBox.Builder SelectBoxField(this BuilderFactory input)
        {
            var sharpDataList = new ForeignSelectBox();

            return sharpDataList.ToBuilder();
        }
        /// <summary>
        /// 阅读器
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static AjaxProxy.Builder Reader(this AjaxProxy.Builder input)
        {
            return input.Reader(Html.X().JsonReader().RootProperty("data"));
        }

        /// <summary>
        /// 默认值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static AjaxProxy.Builder Post(this AjaxProxy.Builder input)
        {
            input.ActionMethods(actions =>
            {
                actions.Read = HttpMethod.POST;
            });

            return input;
        }
        /// <summary>
        /// 过滤器
        /// </summary>
        /// <param name="input"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static Column.Builder Filter(this ColumnBase input, GridFilter filter)
        {
            return ((Column.Builder)input).Filter(filter);
        }

        /// <summary>
        /// 隐藏显示触发器
        /// </summary>
        /// <param name="input"></param>
        /// <param name="hide"></param>
        /// <returns></returns>
        public static FieldTrigger.Builder<FieldTrigger, FieldTrigger.Builder> HideTrigger(
            this FieldTrigger.Builder<FieldTrigger, FieldTrigger.Builder> input, bool hide)
        {
            return input;
        }
        /// <summary>
        /// Ext.net Local
        /// </summary>
        /// <param name="input"></param>
        /// <param name="hide"></param>
        /// <returns></returns>
        public static GridFilters.Builder Local(this GridFilters.Builder input, bool hide)
        {
            return input;
        }
        /// <summary>
        /// Ext.net 过滤器
        /// </summary>
        /// <param name="input"></param>
        /// <param name="objects"></param>
        /// <returns></returns>
        public static GridFilters.Builder Filters(this GridFilters.Builder input, params object[] objects)
        {
            return input;
        }
        /// <summary>
        /// 自动生成Id
        /// </summary>
        /// <typeparam name="TComponent"></typeparam>
        /// <typeparam name="TBuilder"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static TBuilder AutoId<TComponent, TBuilder>(this ControlBuilder<TComponent, TBuilder> input)
            where TComponent : Control where TBuilder : ControlBuilder<TComponent, TBuilder>
        {
            var id = "field" + Guid.NewGuid().ToString("N");
            return input.ID(id);
        }
        /// <summary>
        /// 根据配置加载列表页
        /// </summary>
        /// <param name="input"></param>
        /// <param name="listPage"></param>
        /// <returns></returns>
        public static GridPanel.Builder SharpReconfigure(this GridPanel.Builder input, SharpList listPage)
        {
            var grid = input.ToComponent();

            var newList = listPage.GetCustomColumns().Columns;
            if (!newList.Any())
            {
                return input;
            }
            //var columns = new ColumnBase[grid.ColumnModel.Columns.Count(x => x.GetType() != typeof(RowNumbererColumn))];
            //grid.ColumnModel.Columns.Where(x => x.GetType() != typeof(RowNumbererColumn)).ToList().CopyTo(columns);


            //grid.ColumnModel.Columns.RemoveAll(x => x.GetType() != typeof(RowNumbererColumn));

            //for (var index = 0; index < newList.Count; index++)
            //{
            //    var column = columns.FirstOrDefault(x => x.DataIndex == newList[index].Name);
            //    if (column == null) continue;
            //    column.Hidden = newList[index].Hidden;
            //    column.Width = newList[index].Width;

            //    grid.ColumnModel.Columns.Add(column);
            //}

            //var outList = columns.Where(x => newList.All(y => y.Name != x.DataIndex));
            //grid.ColumnModel.Columns.AddRange(outList);


            foreach (var column in grid.ColumnModel.Columns)
            {
                var configcolumn = newList.FirstOrDefault(x => x.Name == column.DataIndex);
                if (configcolumn == null) continue;

                column.Hidden = configcolumn.Hidden;
                if (configcolumn.Width > 0)
                    column.Width = configcolumn.Width;

            }

            return input;
        }
        /// <summary>
        /// 根据配置加载列表页
        /// </summary>
        /// <param name="input"></param>
        /// <param name="listPage"></param>
        /// <returns></returns>
        public static TreePanel.Builder SharpReconfigure(this TreePanel.Builder input, SharpList listPage)
        {
            var grid = input.ToComponent();

            var newList = listPage.GetCustomColumns().Columns;
            if (!newList.Any())
            {
                return input;
            }
            foreach (var column in grid.ColumnModel.Columns)
            {
                var configcolumn = newList.FirstOrDefault(x => x.Name == column.DataIndex);
                if (configcolumn == null) continue;

                column.Hidden = configcolumn.Hidden;
                if (configcolumn.Width > 0)
                    column.Width = configcolumn.Width;

            }

            return input;
        }
        /// <summary>
        /// 响应式配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static GridPanel.Builder ResponsiveConfig(this GridPanel.Builder input)
        {
            input.ResponsiveConfig(rr =>
            {
                var panelRule = new ResponsiveRule()
                {
                    Rule = "width<=1024"
                };
                panelRule.Config.Add(Html.X().GridPanel().SelectionModel(Html.X().RowSelectionModel()));
                //rr.Add(panelRule);
            });

            return input;
        }
        /// <summary>
        /// SearchArea响应式配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static FormPanel.Builder SearchAreaResponsiveConfig(this FormPanel.Builder input)
        {
            input.ResponsiveConfig(rr =>
            {
                var pcRule = new ResponsiveRule()
                {
                    Rule = "width>1024"
                };
                pcRule.Config.Add(Html.X().FormPanel().Layout(LayoutType.Column));
                rr.Add(pcRule);
                var panelRule = new ResponsiveRule()
                {
                    Rule = "width<=1024"
                };
                panelRule.Config.Add(Html.X().FormPanel().Layout(LayoutType.Form));
                rr.Add(panelRule);
            });
            return input;
        }
        /// <summary>
        /// Window响应式配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Window.Builder WindowResponsiveConfig(this Window.Builder input)
        {
            input.ResponsiveConfig(rr =>
            {
                var panelRule = new ResponsiveRule()
                {
                    Rule = "width<=1024"
                };
                panelRule.Config.Add(Html.X().Window().Layout(LayoutType.Table).Maximized(true).AutoScroll(true));
                //rr.Add(panelRule);
            });
            return input;
        }
    }
}